<template>
  <!-- 空白页 -->
</template>

<script lang="ts" setup>
import { onMounted } from "vue";
import { bindEmail, unbindEmail } from "@/api/authority/user";
import { useRoute, useRouter } from "vue-router";
import { setInfo } from "@/views/result/ResultStorage";
import { ResultEnum } from "@/enums/httpEnum";

const route = useRoute();
const router = useRouter();

const map = new Map<string, Function>([
  ['bind_email', async (data, code) => {
    const res = await bindEmail(data, code);
    if (res.code === ResultEnum.SUCCESS) {
      setInfo({
        status: 'success',
        title: '操作成功',
        description: '用户绑定邮箱的处理结果',
        content: res.message,
        createTime: Date.now()
      });
      return true;
    }
    return false;
  }],
  ['unbind_email', async (data, code) => {
    const res = await unbindEmail(data, code);
    if (res.code === ResultEnum.SUCCESS) {
      setInfo({
        status: 'success',
        title: '操作成功',
        description: '用户解绑邮箱的处理结果',
        content: res.message,
        createTime: Date.now()
      });
      return true;
    }
    return false;
  }]
]);

onMounted(async () => {
  const { type, data, code } = route.params as any;
  if (map.has(type)) {
    // @ts-ignore
    const ok = await map.get(type)(data, code);
    if (!ok) {
      setInfo({
        status: 'error',
        title: '操作失败',
        description: '请正确访问给定的操作链接',
        content: '非法请求, 有问题请联系管理员',
        createTime: Date.now()
      });
    }
  } else {
    setInfo({
      status: 'error',
      title: '操作失败',
      description: '未匹配到操作类型',
      content: '未知的操作类型',
      createTime: Date.now()
    });
  }
  return await router.push('/result');
})
</script>

<style scoped>

</style>
